package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.stock.EasyPartBean;
import com.scisoft.scan.data.stock.PartSupplierBean;
import com.scisoft.scan.data.stock.StockFlowBean;
import com.scisoft.scan.data.stock.StockFlowQueryBean;
import com.scisoft.scan.data.stock.StockInfoBean;
import com.scisoft.scan.data.stock.StockInfoCheckBean;
import com.scisoft.scan.data.stock.StockInfoMoveBean;
import com.scisoft.scan.data.stock.StockInfoPictureBean;
import com.scisoft.scan.data.stock.StockInfoPictureUrlBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_Stock {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/StockService?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://stock.scan.ws.dms.com/";

    //查询库存列表 bizId:模糊搜索的条件 pagesize 每页的数量 pageindex 第几页,页码应该从1开始
    public static List<StockInfoBean> STOCK_01(String bizId, int pagesize, int pageindex) throws Exception {

       /* List<StockInfoBean> ret = new ArrayList<>();
        StockInfoBean s1 = new StockInfoBean();
        s1.partName = "汽车车窗";
        s1.partId = "P0001";
        s1.supplierCode = "DY01";
        s1.supplierName = "大运";
        s1.position = "K1";
        s1.depotName = "J仓";
        s1.areaName = "K区";
        s1.stockCount = "100";
        s1.canUseCount = "80";
        ret.add(s1);
        return  ret;*/
        //以下代码可以复用，模糊查询，看看根据业务是否可以支持配件代码、配件名称、供应商等
        List<StockInfoBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCK_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
       if (msg!=null&&msg.contains("成功"))
       {
           JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<StockInfoBean>>(){}.getType());
       }
        else
      {
           throw new Exception(msg);
        }
        return ret;
    }

    //查询库存流水 bizId:配件编号
    public static StockFlowBean STOCK_02(StockFlowQueryBean stockFlowQueryBean) throws Exception {

        StockFlowBean ret = new StockFlowBean();
        ret.partNo = "P01";
        ret.partName = "汽车车窗";
        ret.manufacturerName = "大运汽车";
        return ret;

        //下面接口可以复用，需要考虑一下，查询库存流水是否按照仓库、供应商？根据需求来调整接口
        /*
        String arg0 = String.format("{\"flowNo\":\"STOCK_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, stockFlowQueryBean.partNo);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        StockFlowBean ret = new StockFlowBean();
        if (msg != null && msg.contains("成功")) {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<StockFlowBean>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
        */
    }

    //上传配件图片
    public static String STOCK_04(StockInfoPictureBean stockInfoPictureBean) throws Exception {

//        return "成功";
        //下面代码可以复用
        Gson gson = new Gson();
        String parm =gson.toJson(stockInfoPictureBean);
        String arg0 = String.format("{\"flowNo\":\"STOCK_04\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询库存的分页信息 bizId:模糊搜索的条件
    public static QueryPageBean STOCK_05(String bizId) throws Exception {
//        QueryPageBean ret = new QueryPageBean();
//        ret.pageCount = 2;
//        ret.totalCount = 18;
//        ret.pageSize = 10;
//        return ret;
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"STOCK_05\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
       else
        {
            throw new Exception(msg);
        }
        return ret;

    }

    //配件移库
    public static String STOCK_06(StockInfoMoveBean stockInfoMoveBean) throws Exception {
        return "成功";
    }

    //配件盘点
    public static String STOCK_07(StockInfoCheckBean stockInfoCheckBean) throws Exception {

        return "成功";
        //以下可以复用
        /*
        QuickCheckDetailBean quickCheckDetailBean =new QuickCheckDetailBean();
        quickCheckDetailBean.stockId=stockInfoCheckBean.stockId;
        quickCheckDetailBean.checkCount=stockInfoCheckBean.checkCount;
        //生成盘点单
        String orderId =WebServiceHelper_QuickCheck.QUICKCHECK_02(quickCheckDetailBean,null,"");
        String ret="";
        //生成盘点调整单
        if(orderId.length()>0){
            ret=WebServiceHelper_Check.CHECK_04(orderId);
        }
        return ret;
        */
    }

    //删除配件图片 bizId:配件图片编号
    public static String STOCK_09(String bizId)throws Exception {

//        return "成功";
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"STOCK_09\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }

    //查询配件图片 bizId:配件编号
    public static List<StockInfoPictureUrlBean> STOCK_10(String bizId)throws Exception {

//        List<StockInfoPictureUrlBean> ret = new ArrayList<>();
//        StockInfoPictureUrlBean s1 = new  StockInfoPictureUrlBean();
//        s1.partPictureId = "P01";
//        s1.lableId = "kkk";
//        s1.partPictureUrl = "";
//        return ret;

        //以下代码可以复用
        List<StockInfoPictureUrlBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCK_10\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<StockInfoPictureUrlBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //配件查询接口：仅查询配件信息，不区分供应商和唯一码
    public static QueryPageBean STOCK_08(String bizId) throws Exception{
//        QueryPageBean ret = new QueryPageBean();
//        ret.pageCount = 2;
//        ret.totalCount = 18;
//        ret.pageSize = 10;
//        return ret;
        String arg0 = String.format( "{\"flowNo\":\"STOCK_08\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //配件查询接口：仅查询配件信息，不区分供应商和唯一码.bizId 查询条件
    public static List<EasyPartBean> STOCK_11(String bizId, int pagesize, int pageindex) throws Exception {
//        List<EasyPartBean> ret = new ArrayList<>();
//        EasyPartBean p1 = new EasyPartBean();
//        p1.partCode = "P001";
//        p1.partId = "P001";
//        p1.partName = "汽车方向盘";
//        p1.partNo = "PF-001";
//        ret.add(p1);
//        return ret;
        List<EasyPartBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCK_11\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<EasyPartBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    /*
     * 根据配件id查询库存列表 bizId 传配件id
     * 首先扫码然后通过服务查询到配件的ID，再调用此服务，通过配件id来查询配件的库存信息。此服务能够区分供应商
     */
    public static List<StockInfoBean> STOCK_12(String bizId) throws Exception {

//        List<StockInfoBean> ret =  new ArrayList<>();
//        StockInfoBean s1 = new StockInfoBean();
//        s1.depotName = "J库";
//        s1.areaName = "A区";
//        s1.position = "J-001";
//        s1.stockCount = "50";
//        s1.canUseCount = "20";
//        s1.occupyCount = "30";
//        s1.supplierCode = "DY";
//        s1.supplierName = "大运汽配";
//        ret.add(s1);
//        return ret;

        //以下代码可以复用
        List<StockInfoBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCK_12\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<StockInfoBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    /*
     * 根据配件partNo查询库存列表 bizId 传配件partNo
     * 首先扫码然后通过服务查询到配件的PartNo，再调用此服务，通过配件partNo来查询配件的库存信息。此服务能够查询一个配件所有供应商的库存列表
     */
    public static List<StockInfoBean> STOCK_13(String partId) throws Exception {

//        List<StockInfoBean> ret = new ArrayList<>();
//        StockInfoBean s1 = new StockInfoBean();
//        s1.partNo = "P01-109";
//        s1.partName = "汽车车窗";
//        s1.depotName = "J库";
//        s1.areaName = "A区";
//        s1.position = "J-001";
//        s1.stockCount = "50";
//        s1.canUseCount = "20";
//        s1.occupyCount = "30";
//        s1.supplierCode = "DY";
//        s1.supplierName = "大运汽配";
//        ret.add(s1);
//        return ret;
        //以下代码可以复用
        List<StockInfoBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCK_13\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,partId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<StockInfoBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    /*
     * 根据配件partNo查询供应商报价表 bizId 传配件partNo
     */
    public static List<PartSupplierBean> STOCK_14(String partId)throws Exception {

//        List<PartSupplierBean> ret = new ArrayList<>();
//        PartSupplierBean s1 = new PartSupplierBean();
//        s1.partNo = "P01";
//        s1.partName = "汽车车窗";
//        s1.manufacturer = "DY";
//        s1.manufacturerName = "大运汽配";
//        s1.price = "5200";
//        ret.add(s1);
//        return ret;
        List<PartSupplierBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"STOCK_14\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,partId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<PartSupplierBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }


    public static SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
